/*
 * Copyright (C) 2008  Wang Hoi <fearee@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
 */
#ifndef GET_OPT_H
#define GET_OPT_H

#include <QtCore>

class GetOpt
{
public:
    GetOpt();
    GetOpt(int offset);
    GetOpt(int argc, char *argv[]);
    GetOpt(const QStringList &a);

    QString appName() const {
        return aname;
    }

    // switch (no arguments)
    void addSwitch(const QString &lname, bool *b);

    // options (with arguments, sometimes optional)
    void addOption(char s, const QString &l, QString *v);
    void addVarLengthOption(const QString &l, QStringList *v);
    void addRepeatableOption(char s, QStringList *v);
    void addRepeatableOption(const QString &l, QStringList *v);
    void addOptionalOption(const QString &l, QString *v,
                           const QString &def);
    void addOptionalOption(char s, const QString &l,
                           QString *v, const QString &def);

    // bare arguments
    void addArgument(const QString &name, QString *v);
    void addOptionalArgument(const QString &name, QString *v);

    bool parse(bool untilFirstSwitchOnly);
    bool parse() {
        return parse(false);
    }

    bool isSet(const QString &name) const;

    int currentArgument() const {
        return currArg;
    }

private:
    enum OptionType { OUnknown, OEnd, OSwitch, OArg1, OOpt, ORepeat, OVarLen };

    struct Option;
    friend struct Option;

    struct Option {
        Option(OptionType t = OUnknown,
               char s = 0, const QString &l = QString::null)
                : type(t),
                sname(s),
                lname(l),
                boolValue(0) { }

        OptionType type;
        char sname;     // short option name (0 if none)
        QString lname;  // long option name  (null if none)
        union {
            bool *boolValue;
            QString *stringValue;
            QStringList *listValue;
        };
        QString def;
    };

    QList<struct Option> options;
    typedef QList<struct Option>::ConstIterator OptionConstIterator;
    QMap<QString, int> setOptions;

    void init(int argc, char *argv[], int offset = 1);
    void addOption(Option o);
    void setSwitch(const Option &o);

    QStringList args;
    QString aname;

    int numReqArgs;
    int numOptArgs;
    Option reqArg;
    Option optArg;

    int currArg;
};

#endif

